import { Component,Node } from "cc";
import BaseView from "./BaseView";
import BaseEventMgr from "../event/BaseEventMgr";
import { LogMgr } from "../log/LogMgr";


export class UIComponent extends Component{
    _owerView:BaseView<any,any> = null

    // 管理所有UI组件
    _uiComponents: Set<UIComponent> = new Set();

    public get owerView(){
        return this._owerView
    }
    public set owerView(view:BaseView<any,any>){
        this._owerView = view
    }

    // 注册组件 (改为public)
    public registerComponent(comp: UIComponent,owerView:BaseView<any,any>) {
        this._uiComponents.add(comp);
        comp.owerView = owerView;
    }

    // 注销组件
    public unregisterComponent(comp: UIComponent) {
        this._uiComponents.delete(comp);
        comp.owerView = null
    }


    protected __preload(): void {
        this.nodeBindHandle();
    }
    nodeBindHandle() {
        let clazzNmae = this.constructor.name
        
        let binds = Object.getPrototypeOf(this).constructor.prototype["__binds"]
        if(!binds){
            return
        }
        let keyNum = 0
        let bindPropertyKeys = []
        for(let key in binds) {
            keyNum++
            bindPropertyKeys.push(key)
        }
        if(keyNum===0){
            return
        }
        let option = {
            findNum:0,
            keyNum: keyNum
        }

        this.findNode(this.node.children,binds,bindPropertyKeys,option)
    }
    findNode(
        children:Node[],
        binds:{
            [key:string]:string
        },
        bindPropertyKeys:string[],
        option:{
            findNum:number,
            keyNum:number
        }
    ){
        if(option.findNum===option.keyNum){
            return
        }
        
        children.forEach(n=>{
            let cmps = n.components
            for(let i=0;i<cmps.length;i++){
                if(cmps[i] instanceof UIComponent){//不继续查找子节点
                    return
                }
            }

            let nodeName = n.name
            bindPropertyKeys.forEach(key=>{
                let targetMeta = binds[key]
                let targetNodeName = targetMeta["nodeName"]
                let targetCmpType = targetMeta["cmpType"]
                if(targetNodeName===nodeName){
                    let old = this[key]
                    if(old){
                        let oldValuePath = ""
                        if(old instanceof Node){
                            oldValuePath = (old as Node).getPathInHierarchy()
                        }else{
                            oldValuePath = (old.node as Node).getPathInHierarchy()
                        }
                        LogMgr.warn("绑定节点名重复："+nodeName+" 视图："+this.name+" 字段："+key+" oldPath:"+oldValuePath+" nowPath:"+n.getPathInHierarchy())
                        return
                    }
                    if(Node===targetCmpType){
                        this[key] = n
                    }else{
                        this[key] = n.getComponent(targetCmpType) as Component
                    }
                    option.findNum++
                }
            })

            let children = n.children
            if(children.length>0){
                this.findNode(children,binds,bindPropertyKeys,option)
            }
        })
    }
}